<template>
  <div class="my-container">
    <!-- 已登录--- 头部 -->
    <div v-if="user" class="userInfo header">
      <div class="base">
        <div class="left">
          <van-image class="avatar" fit="cover" round :src="userInfo.photo" />
          <span class="name">{{userInfo.name}}</span>
        </div>
        <div class="right">
          <van-button to="/user/profile" type="default" size="mini" round>编辑资料</van-button>
        </div>
      </div>
      <!-- 粉丝、关注 -->
      <div class="data">
        <div class="data-item">
          <span>{{userInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>

    <!-- 未登录----头部 -->
    <div v-else class="header1 header">
      <img class="mobile-img" src="@/assets/mobile.png" @click="$router.push('/login')">
      <span>登录 / 注册</span>
    </div>

    <!-- 主体 -->
    <van-grid class="nav-grid" :column-num="2" clickable>
      <van-grid-item class="header" text='收藏'>
        <i slot="icon" class="toutiao toutiao-shoucang" />
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="header" text='历史'>
        <i slot="icon" class="toutiao toutiao-lishi" />
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>
    <van-cell-group class="cell-box">
      <van-cell title="消息通知" is-link />
      <!-- <van-cell title="实名认证" is-link />
      <van-cell title="用户反馈" is-link /> -->
      <van-cell title="小智同学" is-link />
      <!-- <van-cell title="系统反馈" is-link /> -->
    </van-cell-group>

    <!-- 退出 -->
    <van-cell v-if="user" class="loginout cell-box" title="退出" center @click="onLogout" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
// 调用接口
import { getUserInfo } from '@/api/user.js'
export default {
  name: 'My-page',
  data () {
    return {
      // 用户信息
      userInfo: {}
    }
  },
  methods: {
    // 获取用户信息
    async loadUser () {
      try {
        const { data: res } = await getUserInfo()
        console.log(res.data)
        this.userInfo = res.data
      } catch (err) {
        console.log(err)
      }
    },
    // 退出登录
    onLogout () {
      this.$dialog.confirm({
        title: '黑马头条',
        message: '确认退出此账号吗?'
      }).then(() => {
        // 确认退出  清楚本地
        this.$store.commit('setUser', null)
      }).catch(err => {
        console.log(err)
      })
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  filters: {},
  components: {},
  created () {
    // 本地有缓存数据  直接加载
    if (this.user) {
      this.loadUser()
    }
  },
  mounted () { }
}
</script>

<style lang='less' scoped>
.my-container {
  .header {
    height: 400px;
    background: url('~@/assets/banner.png') no-repeat;
    background-size: cover;
  }
  .header1 {
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    .mobile-img {
      width: 132px;
      height: 132px;
    }
    span {
      margin-top: 9px;
      font-size: 28px;
      color: #fff;
    }
  }
  .userInfo {
    // background-color: red;
    .base {
      height: 231px;
      padding: 76px 32px 23px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      // background-color: #ccc;
      box-sizing: border-box;
      .left {
        display: flex;
        justify-items: center;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          border: 2px solid #fff;
        }
        .name {
          color: #fff;
          font-size: 36px;
          margin-left: 20px;
        }
        .right {
          font-size: 29px;
          margin-left: 22px;
        }
      }
    }
    .data {
      display: flex;
      justify-content: space-around;
      .data-item {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 130px;
        flex-direction: column;
        color: #fff;
      }
      span:nth-child(1) {
        font-size: 36px;
        margin-bottom: 15px;
      }
      span:nth-child(2) {
        font-size: 23px;
      }
    }
  }
  .nav-grid {
    background-color: #fff;
    .van-grid-item {
      height: 178px;
      .toutiao {
        font-size: 45px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
      .text {
        margin-top: 8px;
        font-size: 28px;
      }
    }
  }
  .cell-box {
    margin: 10px 0;
  }
  .loginout {
    height: 100px;
    color: #eb5253;
    font-size: 30px;
    text-align: center;
  }
}
</style>
